
/************************************************************************
 * @file PluginVersion.cpp
 * @author Ian Chen
 * @version 1.0
 *
 * @section LICENSE
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
 * General Public License for more details at
 * http://www.gnu.org/licenses/lgpl.html
 ************************************************************************/

#include <QString>
#include <QStringList>
#include <plugin/util/PluginVersion.h>

namespace antu
{
namespace plugin
{
PluginVersion::PluginVersion()
{
    PluginVersion(DEFAULT_MAJOR_VERSION, DEFAULT_MINOR_VERSION,
                  DEFAULT_PATCH_LEVEL, DEFAULT_BUILD_LEVEL);
}

PluginVersion::PluginVersion(const QString &version)
{
    *this = PluginVersion::toVersion(version);
}

PluginVersion::PluginVersion(const int major, const int minor, const int patch, const int build)
    : m_iMajorVersion(major),
      m_iMinorVersion(minor),
      m_iPatchVersion(patch),
      m_iBuild(build)
{
}

bool PluginVersion::operator ==(const PluginVersion &version) const
{
    return (this->majorVersion() == version.majorVersion() &&
            this->minorVersion() == version.minorVersion() &&
            this->patchLevel() == version.patchLevel() &&
            this->buildLevel() == version.buildLevel());
}

bool PluginVersion::operator >=(const PluginVersion &version) const
{
    return !(*this < version);
}

bool PluginVersion::operator !=(const PluginVersion &version) const
{
    return !(*this == version);
}

bool PluginVersion::operator <=(const PluginVersion &version) const
{
    return (*this == version || *this < version);
}

bool PluginVersion::operator >(const PluginVersion &version) const
{
    return !(*this <= version);
}

bool PluginVersion::operator <(const PluginVersion &version) const
{
    if (this->majorVersion() > version.majorVersion())
        return false;
    if (this->majorVersion() < version.majorVersion())
        return true;

    if (this->minorVersion() > version.minorVersion())
        return false;
    if (this->minorVersion() < version.minorVersion())
        return true;

    if (this->patchLevel() > version.patchLevel())
        return false;
    if (this->patchLevel() < version.patchLevel())
        return true;

    if (this->buildLevel() >= version.buildLevel())
        return false;

    return true;
}

PluginVersion &PluginVersion::operator =(const PluginVersion &version)
{
    this->m_iMajorVersion = version.majorVersion();
    this->m_iMinorVersion = version.minorVersion();
    this->m_iPatchVersion = version.patchLevel();
    this->m_iBuild = version.buildLevel();
    return *this;
}

PluginVersion PluginVersion::toVersion(const QString &versionString)
{
    int major = DEFAULT_MAJOR_VERSION, minor = DEFAULT_MINOR_VERSION, patch = DEFAULT_PATCH_LEVEL, build = DEFAULT_BUILD_LEVEL;
    QStringList list = versionString.split(".", QString::KeepEmptyParts);

    bool ok = false;
    major = list.at(0).toInt(&ok);

    if (ok && list.size() > 1)
    {
        ok = false;
        minor = list.at(1).toInt(&ok);

        if (ok && list.size() > 2)
        {
            ok = false;
            patch = list.at(2).toInt(&ok);

            if (ok && list.size() > 3)
            {
                ok = false;
                build = list.at(3).toInt(&ok);

                if (!ok)
                {
                    build = DEFAULT_BUILD_LEVEL;
                }
            }
            else
            {
                patch = DEFAULT_PATCH_LEVEL;
            }
        }
        else
        {
            minor = DEFAULT_MINOR_VERSION;
        }
    }
    else
    {
        major = DEFAULT_MAJOR_VERSION;
    }
    return PluginVersion(major, minor, patch, build);
}

QString PluginVersion::toString() const
{
    QString content = QString("%1.%2.%3.%4").arg(this->majorVersion())
        .arg(this->minorVersion())
        .arg(this->patchLevel())
        .arg(this->buildLevel());
    return content;
}

}
}
